<?php
namespace Admin\Controller;

use Admin\Model\B2cOrdersModel;

/**
 * @package Admin\Controller
 */

class QgsOrdersController extends BaseController  {

    public $local_id;

    public function __construct()
    {
        parent::__construct();

        $this->local_id = session('userInfo')['local_id'];
    }
    /**
     * 订单列表
     */
    public function orderList(){
        $p = I('p',1,'intval');
        $sort = I('get.sort','', htmlentities());
        $search = I('get.search','', htmlentities());
        $searchVal = I('get.search_val','', htmlentities());

        $Model = new B2cOrdersModel();
        $orderList = $Model->qgsOrderList($p, $sort, $search, $searchVal);

        $this->assign('view', ['sort'=>$sort, 'search'=>$search, 'searchVal'=>$searchVal]);
        $this->assign('page',$orderList['page']);// 赋值分页输出
        $this->assign('orders',$orderList['orders']);//赋值订单列表
        $this->display('Orders/qgsOrderList');
    }

    /**
     *
     * 订单详情
     *
     */

    public function  orderDetail(){

        $orderId = I('get.id');
        $Model = new B2cOrdersModel();

        $orDetail = $Model->qgsOrderDetail($orderId);

        //售后申请商品
        foreach ($orDetail['product'] as $k=>$value){

            $is_refund[$k] = M('b2c_refund_money_goods mg')
                ->join("left join ".C('DB_PREFIX')."b2c_orders o on o.order_id = mg.order_id")
                ->join("left join ".C('DB_PREFIX')."b2c_refund_money m on m.id = mg.refund_money_id")
                ->join("left join ".C('DB_PREFIX')."b2c_order_items it on it.item_id = mg.item_id")
                ->where(['m.order_id'=>$value['order_id'],'mg.item_id'=>$value['item_id'],'o.relation_local_id'=>session('userInfo.local_id'),'m.status'=>['not in','0,6,7'],
                'm.type'=>'1'])
                ->field('it.*,m.price as backprice,mg.num')
                ->order('m.id desc')
                ->find();
            if(empty($is_refund[$k])){
                unset($is_refund[$k]);
                continue;
            }


            $is_refund[$k]['istype'] = '发货前：退款';
            $is_refund[$k]['bn'] = $value['bn'];
            $is_refund[$k]['name'] = $value['name'];
            $is_refund[$k]['barcode'] = $value['barcode'];
            $is_refund_tree[$k]['price'] = $value['price'];
            $is_refund[$k]['sendnum'] = $value['sendnum'];
            $is_refund[$k]['nums'] = $value['nums'];
            $is_refund[$k]['amount'] = $value['amount'];

        }
        //发货后 售后退款
        foreach ($orDetail['product'] as $k=>$value){

            $is_refund_two[$k] = M('b2c_refund_money_goods mg')
                ->join("left join ".C('DB_PREFIX')."b2c_orders o on o.order_id = mg.order_id")
                ->join("left join ".C('DB_PREFIX')."b2c_refund_money m on m.id = mg.refund_money_id")
                ->join("left join ".C('DB_PREFIX')."b2c_order_items it on it.item_id = mg.item_id")
                ->where(['m.order_id'=>$value['order_id'],'mg.item_id'=>$value['item_id'],'o.local_id'=>session('userInfo.local_id'),'m.status'=>['not in','0,6,7'],
                    'm.type'=>'2'])
                ->field('it.*,m.price as backprice,mg.num')
                ->order('m.id desc')
                ->find();
            if(empty($is_refund_two[$k])){
                unset($is_refund_two[$k]);
                continue;
            }


            $is_refund_two[$k]['istype'] = '发货后：售后退款';
            $is_refund_two[$k]['bn'] = $value['bn'];
            $is_refund_two[$k]['name'] = $value['name'];
            $is_refund_tree[$k]['price'] = $value['price'];
            $is_refund_two[$k]['barcode'] = $value['barcode'];
            $is_refund_two[$k]['sendnum'] = $value['sendnum'];
            $is_refund_two[$k]['nums'] = $value['nums'];
            $is_refund_two[$k]['amount'] = $value['amount'];

        }

         //发货后 售后退货退款
        foreach ($orDetail['product'] as $k=>$value){

            $is_refund_tree[$k] = M('b2c_refund_money_goods mg')
                ->join("left join ".C('DB_PREFIX')."b2c_orders o on o.order_id = mg.order_id")
                ->join("left join ".C('DB_PREFIX')."b2c_refund_money m on m.id = mg.refund_money_id")
                ->join("left join ".C('DB_PREFIX')."b2c_order_items it on it.item_id = mg.item_id")
                ->where(['m.order_id'=>$value['order_id'],'mg.item_id'=>$value['item_id'],'o.relation_local_id'=>session('userInfo.local_id'),'m.status'=>['not in','0,6,7'],
                    'm.type'=>'3'])
                ->field('it.*,m.price as backprice,mg.num')
                ->order('m.id desc')
                ->find();

            if(empty($is_refund_tree[$k])){
                unset($is_refund_tree[$k]);
                continue;
            }
            $is_refund_tree[$k]['istype'] = '发货后：退货退款';
            $is_refund_tree[$k]['bn'] = $value['bn'];
            $is_refund_tree[$k]['name'] = $value['name'];
            $is_refund_tree[$k]['price'] = $value['price'];
            $is_refund_tree[$k]['barcode'] = $value['barcode'];
            $is_refund_tree[$k]['sendnum'] = $value['sendnum'];
            $is_refund_tree[$k]['nums'] = $value['nums'];
            $is_refund_tree[$k]['amount'] = $value['amount'];

        }

        if($orDetail['status']===0){
            echo "<script>alert('参数错误');history.go(-1);</script>";
            return false;
        }
        $refundInfo = $Model->getRefundDetail($orderId);
        $this->assign('refundInfo',$refundInfo);
        $this->assign('orDetail',$orDetail);
        $this->assign('isrefund',$is_refund);
        $this->assign('isrefundtwo',$is_refund_two);
        $this->assign('isrefundtree',$is_refund_tree);
        $this->display('Orders/qgsOrderDetail');
    }

    /**
     * 订单日志
     */
    public function orderLog(){
        $Model = new B2cOrdersModel();

        $result = $Model->qgsOrderLog(I('get.id'));

        if($result['status']===0){
            echo "<script>alert('参数错误');history.go(-1);</script>";
            return false;
        }

        $this->assign('order_id',I('get.id'));
        $this->assign('logList',$result);
        $this->display('Orders/qgsOrderLog');
    }
}